package com.cgsd.heyu.utils;

import com.android.library.utils.ToastHelper;
import com.cgsd.heyu.common.FileTokenConfig;
import com.cgsd.heyu.common.UserModeConfig;
import com.cgsd.heyu.entity.UploadTokenEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxing
 * Description 上传文件工具类
 * CreateData: 2020/11/17
 */
public class UploadUtils {
    /**
     * 创建图片文件名
     * @return
     */
    public static String createImageName(){
        UploadTokenEntity tokenEntity = FileTokenConfig.getInstance().imageTokenData();
        if (tokenEntity == null){
            ToastHelper.showDefaultToast("未知错误，请退出app重试");
            return null;
        }
        return tokenEntity.fileKey+"img_"+ UserModeConfig.getInstance().userId+"_"+System.currentTimeMillis()+".jpeg";
    }

    /**
     * 创建多媒体文件名
     * @return
     */
    public static String createVoiceName(){
        UploadTokenEntity tokenEntity = FileTokenConfig.getInstance().multiMediaTokenData();
        if (tokenEntity == null){
            ToastHelper.showDefaultToast("未知错误，请退出app重试");
            return null;
        }
        return tokenEntity.fileKey+"voice_"+ UserModeConfig.getInstance().userId+"_"+System.currentTimeMillis()+".wav";
    }

    /**
     * 创建多个图片文件名
     * @param size 个数
     * @return
     */
    public static List<String> createImageNameList(int size){
        UploadTokenEntity tokenEntity = FileTokenConfig.getInstance().imageTokenData();
        if (tokenEntity == null){
            ToastHelper.showDefaultToast("未知错误，请退出app重试");
            return new ArrayList<>(size);
        }
        List<String> keys = new ArrayList<>(size);
        String str = tokenEntity.fileKey+"img_"+ UserModeConfig.getInstance().userId+"_"+System.currentTimeMillis();
        for(int i = 0; i < size; i++){
            keys.add(str+i+".jpeg");
        }
        return keys;
    }

    /**
     * 创建多个媒体文件名
     * @param size 个数
     * @return
     */
    public static List<String> createVoiceNameList(int size){
        UploadTokenEntity tokenEntity = FileTokenConfig.getInstance().imageTokenData();
        List<String> keys = new ArrayList<>(size);
        String str = tokenEntity.fileKey+"voice_"+ UserModeConfig.getInstance().userId+"_"+System.currentTimeMillis();
        for(int i = 0; i < size; i++){
            keys.add(str+i+".wav");
        }
        return keys;
    }

    /**
     * 上传完成后的图片地址
     */
    public static String getUploadVoice(String key){
        UploadTokenEntity tokenEntity = FileTokenConfig.getInstance().multiMediaTokenData();
        if (tokenEntity == null){
            ToastHelper.showDefaultToast("未知错误，请退出app重试");
            return null;
        }
        return tokenEntity.domain+key;
    }

    /**
     * 上传完成后的图片地址
     */
    public static List<String> getUploadImages(List<String> keys){
        List<String> data = new ArrayList<>();
        if (keys == null){
            return data;
        }
        UploadTokenEntity tokenEntity = FileTokenConfig.getInstance().imageTokenData();
        if (tokenEntity == null){
            ToastHelper.showDefaultToast("未知错误，请退出app重试");
            return new ArrayList<>();
        }
        for(String key : keys){
            data.add(tokenEntity.domain+key);
        }
        return data;
    }
}
